package com.nanfang51g3.ckz.ui.fragment.home;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.nanfang51g3.ckz.R;
import com.nanfang51g3.ckz.adapter.ext.HomeRightAdapter;
import com.nanfang51g3.ckz.adapter.listener.OnItemClickListener;
import com.nanfang51g3.ckz.domain.RightTypeBean;
import com.nanfang51g3.ckz.event.RefreshTypeData;
import com.nanfang51g3.ckz.event.RxBus;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/5/18.
 */
public class HomeRightFragment extends Fragment {
    @Bind(R.id.home_recycler)
    RecyclerView homeRecycler;
    ArrayList<RightTypeBean> strings = new ArrayList<>();
    HomeRightAdapter homeRightAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home_right_fragment, container, false);
        ButterKnife.bind(this, view);
        homeRightAdapter = new HomeRightAdapter(strings/*, this*/);
        homeRightAdapter.setActivity(getActivity());
        homeRecycler.setHasFixedSize(true);//固定大小
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        homeRecycler.setLayoutManager(linearLayoutManager);
        //间隔线条
//        homeRecycler.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        homeRightAdapter.setOnItemClickListener(new OnItemClickListener<RightTypeBean>() {

            @Override
            public void onClick(View view, RightTypeBean item) {
                /**
                 * @see HomeIndexFragment#onCreateView(LayoutInflater, ViewGroup, Bundle)
                 */
                RxBus.getDefault().post(new RefreshTypeData(item.getTypeName()));
                homeRightAdapter.notifyItem(item.getTypeName(), true);
            }
        });

        homeRecycler.setAdapter(homeRightAdapter);
        return view;
    }

    public void updateUi(ArrayList<RightTypeBean> types) {
        homeRightAdapter.clearList();
        homeRightAdapter.addList(types);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
